﻿using System;
using System.Data.Services.Common;
using System.Data.SqlTypes;
using System.Runtime.Serialization;

namespace Pottery.Model.Infrastructure.Models
{
    /// <summary>
    /// Store invoice
    /// </summary>
    [DataContract]
    [DataServiceEntity]
    public class StoreInvoice : IValidatable, IAssignable, INullable
    {
        #region Constructors
        /// <summary>
        ///  Default constructor. Creates an StoreInvoice with default params.
        /// </summary>
        public StoreInvoice()
        {

        }

        /// <summary>
        /// Constructor with params. Creates a new StoreInvoice. 
        /// </summary>
        /// <param name="number">Store invoice`s number </param>
        /// <param name="invDate">Invoice`s date</param>
        /// <param name="store">Store, witch make current store invoice</param>
        /// <param name="order">Order in store invoice</param>
        /// <param name="storekeeper">Storekeeper employee</param>
        public StoreInvoice(string number, DateTime invDate, Store store, Order order, Employee storekeeper)
        {
            StoreInvoiceNumber = number;
            mInvoiceDate = invDate;
            mCurrentStore = store;
            mOrderInStoreInvoice = order;
            mStoreKeeper = storekeeper;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Store invoice`s number 
        /// </summary>
        [DataMember]
        public string StoreInvoiceNumber
        {
            get { return mStoreInvoiceNumber; }
            set
            {
                if (!IsNull)
                    mStoreInvoiceNumber = value;
            }
        }

        /// <summary>
        /// Invoice`s date
        /// </summary>
        [DataMember]
        public DateTime InvoiceDate
        {
            get { return mInvoiceDate; }
            set
            {
                if (!IsNull)
                    mInvoiceDate = value;
            }
        }

        /// <summary>
        /// Store, witch make current store invoice
        /// </summary>
        [DataMember]
        public Store CurrentStore
        {
            get { return mCurrentStore; }
            set
            {
                if (!IsNull)
                    mCurrentStore = value;
            }
        }

        /// <summary>
        /// Order in store invoice
        /// </summary>
        [DataMember]
        public Order OrderInStoreInvoice
        {
            get { return mOrderInStoreInvoice; }
            set
            {
                if (!IsNull)
                    mOrderInStoreInvoice = value;
            }
        }

        /// <summary>
        /// Storekeeper employee
        /// </summary>
        [DataMember]
        public Employee StoreKeeper
        {
            get { return mStoreKeeper; }
            set
            {
                if (!IsNull)
                    mStoreKeeper = value;
            }
        }
        #endregion

        #region IValidatable implementation
        /// <summary>
        /// Checks instance for data`s validity
        /// </summary>
        /// <returns>checking result</returns>
        public bool Validate()
        {
            return true;
        }
        #endregion

        #region IAssignable implementation
        /// <summary>
        /// Copy data from current object to object in param.
        /// </summary>
        /// <param name="obj">object to change </param>
        public void AssignTo(object obj)
        {
            if (IsNull)
                throw new InvalidOperationException();

            if (obj == null)
                throw new ArgumentNullException();

            if (obj is StoreInvoice)
            {
                StoreInvoice storeInv = obj as StoreInvoice;

                storeInv.mStoreInvoiceNumber = mStoreInvoiceNumber;
                storeInv.mCurrentStore = mCurrentStore;
                storeInv.mOrderInStoreInvoice = mOrderInStoreInvoice;
                storeInv.mStoreKeeper = mStoreKeeper;
                storeInv.mInvoiceDate = mInvoiceDate;

                storeInv.mNull = false;

                return;
            }

            throw new NotSupportedException("Not supported type!");
        }
        #endregion

        #region INullable Implementation

        public bool IsNull
        {
            get { return mNull; }
        }

        public static StoreInvoice Null
        {
            get
            {
                StoreInvoice storinv = new StoreInvoice();
                storinv.mNull = true;
                return storinv;
            }
        }

        #endregion

        #region private Fields
        private string mStoreInvoiceNumber;
        private DateTime mInvoiceDate;
        private Store mCurrentStore;
        private Order mOrderInStoreInvoice;
        private Employee mStoreKeeper;
        private bool mNull = false;
        #endregion
    }
}
